<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\BankModel;

class BankDao extends BaseDao
{
    public static function builder_query($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = BankModel::query();

        $query->when(checkIsHasField($params,'bank_id'),function ($query)use ($params){
            $query->where('bank_id',$params['bank_id']);
        });

        $query->when(checkIsHasField($params,'status'),function ($query)use ($params){
            $query->where('status',$params['status']);
        });

        return $query->orderByDesc('created_at');
    }


    /**
     * @throws SysExceptions
     */
    public static function add_bank($params = [])
    {
        checkForData(
            ['bank_title','bank_logo','status'],
            ['请输入银行名称','请上传LOGO','请选择状态'],
            $params
        );
        if(self::builder_query()->create($params) === false){
            self::error('添加失败');
        }
    }

    /**
     * @throws SysExceptions
     */
    public static function update_bank($params = [])
    {
        checkForData(
            ['bank_title','bank_logo','status','bank_id'],
            ['请输入银行名称','请上传LOGO','请选择状态','请选择银行'],
            $params
        );
        if(self::builder_query(['bank_id' => self::deleteParamId($params,'bank_id')])->update(self::filterFields($params,new BankModel())) === false){
            self::error('修改失败');
        }
    }

    public static function pageinate(array $all): \Illuminate\Contracts\Pagination\LengthAwarePaginator
    {
        return self::builder_query($all)->paginate();
    }

    public static function info(array $all)
    {
        return self::builder_query($all)->first();
    }

    public static function banks(array $array)
    {
        return self::builder_query($array)->get();
    }


}
